<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="模板id" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="coursescheduleTemplateId">
              <a-select v-model="model.coursescheduleTemplateId" placeholder="请选择应用模板"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in templateList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="节次" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courTempAtomSection">
              <j-dict-select-tag type="list" v-model="model.courTempAtomSection" dictCode="cour_sche_atom_section"
                                 placeholder="请选择节次" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="上课时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courTempAtomStartTime">
              <j-time placeholder="请选择上课时间" v-model="model.courTempAtomStartTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="下课时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courTempAtomEndTime">
              <j-time placeholder="请选择下课时间" v-model="model.courTempAtomEndTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="星期一 " :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courTempAtomMonday">
              <j-dict-select-tag type="list" v-model="model.courTempAtomMonday" dictCode="cour_sch_tem_auto_flag"
                                 placeholder="请选择星期一 " />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="周期二 " :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="courTempAtomTuesday">
              <j-dict-select-tag type="list" v-model="model.courTempAtomTuesday" dictCode="cour_sch_tem_auto_flag"
                                 placeholder="请选择周期二 " />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="星期三 " :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="courTempAtomWednesday">
              <j-dict-select-tag type="list" v-model="model.courTempAtomWednesday" dictCode="cour_sch_tem_auto_flag"
                                 placeholder="请选择星期三 " />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="星期四 " :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="courTempAtomThursday">
              <j-dict-select-tag type="list" v-model="model.courTempAtomThursday" dictCode="cour_sch_tem_auto_flag"
                                 placeholder="请选择星期四 " />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="星期五 " :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courTempAtomFriday">
              <j-dict-select-tag type="list" v-model="model.courTempAtomFriday" dictCode="cour_sch_tem_auto_flag"
                                 placeholder="请选择星期五 " />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="星期六 " :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="courTempAtomSaturday">
              <j-dict-select-tag type="list" v-model="model.courTempAtomSaturday" dictCode="cour_sch_tem_auto_flag"
                                 placeholder="请选择星期六 " />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="星期日 " :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courTempAtomSunday">
              <j-dict-select-tag type="list" v-model="model.courTempAtomSunday" dictCode="cour_sch_tem_auto_flag"
                                 placeholder="请选择星期日 " />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="特殊标识" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="courTempAtomSpecialFlag">
              <a-input-number v-model="model.courTempAtomSpecialFlag" placeholder="请输入特殊标识" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { getAllCourseScheduleTem } from '@/api/commonAPI'

export default {
  name: 'CouCourScheTemplateAtomForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        add: '/courschetemplateatom/couCourScheTemplateAtom/add',
        edit: '/courschetemplateatom/couCourScheTemplateAtom/edit',
        queryById: '/courschetemplateatom/couCourScheTemplateAtom/queryById'
      },
      templateList: []
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.initCourseScheTemplate()
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      if (localStorage.length > 0) {
        let localData = JSON.parse(localStorage.getItem('pro__Login_Userinfo'))
        let tenantId = localData.value.tenantId
        this.model.tenantId = tenantId
      }
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    async initCourseScheTemplate() {
      let res = await getAllCourseScheduleTem()
      if (res.success) {
        this.templateList = []
        res.result.forEach(item => {
          this.templateList.push({
            label: item.courscheTempName,
            value: item.id
          })
        })
      }
    }
  }
}
</script>